package com.laikan.reader.bookshelf.adapter.holder;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.view.SimpleTagImageView;
import com.laikan.reader.database.Book;
import com.socks.library.KLog;

/**
 * Created by Administrator on 2016/8/24.
 */
public class BookShelfGridHolder extends RecyclerView.ViewHolder {

    private View mRootView;
    private Book mBook;
    private SimpleTagImageView mBook_Pic;
    private TextView mTv_Name;
    private TextView mTv_Record_Status;
    private ImageView mSecletView = null;
    private ImageView mSelected = null;
    private View mShadow;
    private boolean mIsLongPress;
    private boolean mIsSelect = false;

    public BookShelfGridHolder(View view) {
        super(view);
        mRootView = view;
        initView(mRootView);
    }

    public View initView(View view) {
        KLog.e("BookShelfGridHolder init");
        mBook_Pic = (SimpleTagImageView)view.findViewById(R.id.book_pic);
        mTv_Name = (TextView)view.findViewById(R.id.top_book_name);
        mTv_Record_Status = (TextView)view.findViewById(R.id.tv_read_status);
        mSecletView = (ImageView)view.findViewById(R.id.select);
        mSelected = (ImageView)view.findViewById(R.id.selected);
        mShadow = view.findViewById(R.id.shadow);
        return view;
    }

    public void setData(Book book) {
        this.mBook = book;
        refreshData(book);
    }

    public Book getData() {
        return mBook;
    }

    public void refreshData(Book book) {
        // 1.书的封面
        mBook_Pic.setImageURI(Uri.parse(book.getIcon()));
        // 2.书名
        mTv_Name.setText(book.getName());

        // 4.设置书籍右上角标签文字
        if (book.getIsUpdated()) {
            mTv_Record_Status.setVisibility(View.VISIBLE);
        }else {
            mTv_Record_Status.setVisibility(View.GONE);
        }
        if ("限免".equals(book.getStatus())) {
            mBook_Pic.setTagWidth(15);  // 更新
            mBook_Pic.setTagText("限免");
        }else {
            mBook_Pic.setTagWidth(0);
            mBook_Pic.setTagText("");
        }
        KLog.e("uptade: "+ book.getIsUpdated() +"  "+book.getStatus() +"  "+book.getId() +"   "+ book.getName());
        // 5.设置书籍右上角标签 背景色
        setTagColor(mBook_Pic, mBook_Pic.getTagText());
    }

    /**
     * 设置书籍右上角标签背景色
     *
     * @param pic
     * @param tag
     */
    private void setTagColor(SimpleTagImageView pic, String tag) {
        if ("更新".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_88BB00));
        }else if ("限免".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_3399FF));
        }else if ("一折".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_EE0022));
        }
    }

    public void refreshViewSelectStatus() {
        if (mIsLongPress) {
            if (mIsSelect) {
                mSelected.setVisibility(View.VISIBLE);
                mSecletView.setVisibility(View.GONE);
                mShadow.setBackgroundResource(R.drawable.tv_book_selected_shadow_bg);
                mShadow.findViewById(R.id.shadow).setVisibility(View.VISIBLE);
            }else {
                mSelected.setVisibility(View.GONE);
                mSecletView.setVisibility(View.VISIBLE);
                mShadow.setBackgroundResource(R.drawable.tv_book_un_select_shadow_bg);
                mShadow.setVisibility(View.VISIBLE);
            }
        }else {
            mSecletView.setVisibility(View.GONE);
            mSelected.setVisibility(View.GONE);
            mShadow.findViewById(R.id.shadow).setVisibility(View.GONE);
        }
    }

    public void setDeleteManagerStatus(boolean status) {
        this.mIsLongPress = status;
    }

    public void setSelectStatus(boolean status) {
        this.mIsSelect = status;
    }

    public View getRootView() {
        return mRootView;
    }
}
